﻿#region Imports
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using DirectoryAnalyzer.Enumerations;
#endregion

namespace DirectoryAnalyzer.Controls.Converters
{
    /// <summary>
    /// Provides conversion between Boolean and System.Windows.Visibility
    /// </summary>
    public class VisibilityConverter : IValueConverter
    {
        #region Declarations
        InversionConverter convInversion = new InversionConverter();
        #endregion

        #region Converter Methods
        /// <summary>
        /// Converts a boolean value into System.Windows.Visibility
        /// </summary>
        /// <param name="value">Boolean</param>
        /// <param name="targetType">The type of the binding target property</param>
        /// <param name="parameter">The converter parameter to use</param>
        /// <param name="culture">The converter parameter to use</param>
        /// <returns>System.Windows.Visibility</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool source;
            try
            {
                source = (bool)value;
            }
            catch (Exception)
            {
                return Visibility.Collapsed;
            }

            if (parameter != null)
                if ((string)parameter == "-")
                    source = (bool)convInversion.Convert(source, targetType, eConversionValueType.Boolean, culture);

            switch ((bool)source)
            {
                case true:
                    return Visibility.Visible;
                case false:
                default:
                    return Visibility.Collapsed;
            }
        }

        /// <summary>
        /// Converts System.Windows.Visibility into a boolean value
        /// </summary>
        /// <param name="value">System.Windows.Visibility</param>
        /// <param name="targetType">The type of the binding target property</param>
        /// <param name="parameter">The converter parameter to use</param>
        /// <param name="culture">The converter parameter to use</param>
        /// <returns>Boolean</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility source;
            try
            {
                source = (Visibility)value;
            }
            catch (Exception)
            {
                return false;
            }

            if (parameter != null)
                if ((string)parameter == "-")
                    source = (Visibility)convInversion.Convert(source, targetType, eConversionValueType.Visibility, culture);

            switch (source)
            {
                case Visibility.Visible:
                    return true;
                case Visibility.Collapsed:
                case Visibility.Hidden:
                default:
                    return false;
            }
        }
        #endregion
    }
}
